﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;

namespace QuanLyQuanCafe
{
    public partial class FrmNguyenLieu : Form
    {
        private NguyenLieuBL nguyenlieuBL = new NguyenLieuBL();
        private DonViTinhBL dvtBL = new DonViTinhBL();
        private CT_MonAnBL ct_monanBL = new CT_MonAnBL();
        public FrmNguyenLieu()
        {
            InitializeComponent();
            cbDVT.DataSource = dvtBL.LayDanhSachDVT();
            cbDVT.DisplayMember = "TenDVT";
            cbDVT.ValueMember = "MaDVT";

        }
        private void FrmNguyenLieu_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = nguyenlieuBL.DSNguyenLieuDVT();
            AnHienButton(false);
        }
        private void AnHienButton(bool flag)
        {
            btnThem.Enabled = !flag;
            btnSua.Enabled = !flag;
            btnXoa.Enabled = !flag;
            btnLuu.Enabled = flag;
            btnHuy.Enabled = flag;

            tbMaNL.Enabled = flag;
            tbTenNL.Enabled = flag;
            cbDVT.Enabled = flag;
            tbSoluongton.Enabled = flag;
            dataGridView1.Enabled = !flag;
        }

        private void CapNhatText()
        {
            tbMaNL.Text = "";
            tbTenNL.Text = "";
            tbSoluongton.Text = "";
        }


        private void btnThem_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            CapNhatText();
            tbMaNL.Text = "";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            tbMaNL.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DataTable datatable = ct_monanBL.LayChiTietMonAnTheoMaNguyenLieu(tbMaNL.Text);
            if (datatable.Rows.Count == 0)
            {
                if (MessageBox.Show("Bạn Có Muốn Xóa Nguyên liệu [" + tbTenNL.Text + "] Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    if (nguyenlieuBL.XoaNguyenLieu(tbMaNL.Text))
                        FrmNguyenLieu_Load(sender, e);
            }
            else
            {
                MessageBox.Show("Nguyên Liệu Đang Được Sử Dụng. Không Thể Xóa Nguyên Liệu Này.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private NguyenLieuBO LayThongTinNL()
        {
            NguyenLieuBO nguyenlieuBO = new NguyenLieuBO();
            nguyenlieuBO.MaNL = int.Parse(tbMaNL.Text);
            nguyenlieuBO.TenNL = tbTenNL.Text;
            nguyenlieuBO.SoLuongTon = float.Parse(tbSoluongton.Text);
            nguyenlieuBO.DVT = int.Parse(cbDVT.SelectedValue.ToString());
            return nguyenlieuBO;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                NguyenLieuBO nguyenlieuBO = LayThongTinNL();
                if (nguyenlieuBL.CheckNguyenLieu(nguyenlieuBO.MaNL))
                {
                    if (nguyenlieuBL.SuaNguyenLieu(nguyenlieuBO))
                        FrmNguyenLieu_Load(sender, e);
                }
                else
                {
                    if (nguyenlieuBL.ThemNguyenLieu(nguyenlieuBO))
                        FrmNguyenLieu_Load(sender, e);
                }
            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            AnHienButton(false);
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                tbMaNL.Text = dataGridView1.Rows[row].Cells["MaNL"].Value.ToString();
                tbTenNL.Text = dataGridView1.Rows[row].Cells["TenNL"].Value.ToString();
                tbSoluongton.Text = dataGridView1.Rows[row].Cells["SoLuongTon"].Value.ToString();
                cbDVT.Text = dataGridView1.Rows[row].Cells["TenDVT"].Value.ToString();
            }
            catch
            {

            }
        }

      
    }
}
